<?php
class ReportesController extends AppController {
    public  $name = 'Reportes';     
    public  $uses = null;                                                         
      
    public function facturacion() {
        App::import('Vendor', 'PdataClass', array('file' => 'pchart' . DS . 'pData.class.php'));
        App::import('Vendor', 'PdrawClass', array('file' => 'pchart' . DS . 'pDraw.class.php'));
        App::import('Vendor', 'PimageClass', array('file' => 'pchart' . DS . 'pImage.class.php'));
        $this->loadModel('Pedido');
        
        $filtros = $this->Session->read('facturacion_filtros');
        $datos = $this->Session->read('facturacion_datos');
        
        $lista_x = array();
        $lista_y = array();
        
        foreach ($datos as $item):
            if ($filtros['corte_facturacion'] == 1) {
                array_push($lista_x, $item['mes'].'/'.$item['anio']);
            }
            if ($filtros['corte_facturacion'] == 2) {
                array_push($lista_x, $item['dia'].'/'.$item['mes']);
            }
            array_push($lista_y, $item['importe']);
        endforeach;
        
        $MyData = new pData();  
        $MyData->addPoints($lista_y,"Curva1");
        $MyData->addPoints($lista_x,"Fecha");
        $MyData->setSerieWeight("Curva1",0);
        $MyData->setAxisName(0,"Importe facturado ($)");
        $MyData->setAbscissa("Fecha");
        $MyData->setAbscissaName("Período");
        
        // Create the pChart object 
        $myPicture = new pImage(1000,400,$MyData);
        // Turn off AntiAliasing 
        $myPicture->Antialias = FALSE;
        
        // Borde del grafico
        $myPicture->drawRectangle(0,0,999,399,array("R"=>0,"G"=>0,"B"=>0));
        
        if ($filtros['corte_facturacion'] == 1) $titulo = 'Facturación mensual';
        if ($filtros['corte_facturacion'] == 2) $titulo = 'Facturación diaria';
                    
        // Titulo del grafico
        $font_folder = APP.'Vendor'.DS.'pchart'.DS.'fonts';
        $myPicture->setFontProperties(array("FontName"=>$font_folder.DS."Forgotte.ttf","FontSize"=>16,"R"=>0,"G"=>0,"B"=>0));
        $myPicture->drawText(100,25,$titulo, array('FontSize'=>16,'Align'=>TEXT_ALIGN_BOTTOMLEFT));
        $myPicture->drawText(600, 25, 'Desde: '.$filtros['fecha_desde'].' Hasta: '.$filtros['fecha_hasta'],array("FontSize"=>16,"Align"=>TEXT_ALIGN_BOTTOMLEFT));
        
        // Fuente del gráfico
        $myPicture->setFontProperties(array("FontName"=>$font_folder.DS.'pf_arma_five.ttf',"FontSize"=>7));

        // Defino el area del grafico
        $myPicture->setGraphArea(50,30,980,350);

        // Dibuja escala del grafico
        /* Draw the scale */
        $scaleSettings = array("XMargin"=>10,"YMargin"=>10,"Floating"=>TRUE,"GridR"=>220,"GridG"=>220,"GridB"=>220,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
        $myPicture->drawScale($scaleSettings);
      
        // Turn on AntiAliasing 
        $myPicture->Antialias = TRUE;
        
        // Dibujo el grafico
        $myPicture->drawLineChart();
        $myPicture->drawPlotChart(array("DisplayValues"=>TRUE,"PlotBorder"=>TRUE,"BorderSize"=>2,"Surrounding"=>-60,"BorderAlpha"=>80));
        
        // genero la imagen que muestra la pagina
        $myPicture->autoOutput(APP.'Vendor'.DS.'pchart'.DS.'pictures'.DS.'facturacion.jpeg');        
        exit;
         
    }

    public function rankingproductos() {
        App::import('Vendor', 'PdataClass', array('file' => 'pchart' . DS . 'pData.class.php'));
        App::import('Vendor', 'PdrawClass', array('file' => 'pchart' . DS . 'pDraw.class.php'));
        App::import('Vendor', 'PimageClass', array('file' => 'pchart' . DS . 'pImage.class.php'));
        $this->loadModel('Pedido');
        
        $filtros = $this->Session->read('rankproductos_filtros');
        $datos = $this->Session->read('rankproductos_datos');
        
        $lista_x = array();
        $lista_y = array();
        
        foreach ($datos as $item):
            array_push($lista_x, $item['Producto']['descripcion'].'   ');
            array_push($lista_y, $item[0]['cantidad']);
        endforeach;
        
        $MyData = new pData();  
        $MyData->addPoints($lista_y,"Curva1");
        $MyData->addPoints($lista_x,"Producto");
        $MyData->setAxisName(0,"Cantidad pedida (u)");
        $MyData->setAbscissa("Producto");
        $MyData->setAbscissaName("Producto");
        
        // Create the pChart object 
        $myPicture = new pImage(1000,400,$MyData);
        // Turn off AntiAliasing 
        $myPicture->Antialias = FALSE;
        
        // Borde del grafico
        $myPicture->drawRectangle(0,0,999,399,array("R"=>0,"G"=>0,"B"=>0));
        
        $titulo = 'Productos más pedidos';
                            
        // Titulo del grafico
        $font_folder = APP.'Vendor'.DS.'pchart'.DS.'fonts';
        $myPicture->setFontProperties(array("FontName"=>$font_folder.DS."Forgotte.ttf","FontSize"=>16,"R"=>0,"G"=>0,"B"=>0));
        $myPicture->drawText(100,25,$titulo, array('FontSize'=>16,'Align'=>TEXT_ALIGN_BOTTOMLEFT));
        $myPicture->drawText(600, 25, 'Desde: '.$filtros['fecha_desde'].' Hasta: '.$filtros['fecha_hasta'],array("FontSize"=>16,"Align"=>TEXT_ALIGN_BOTTOMLEFT));
        
        // Fuente del gráfico
        $myPicture->setFontProperties(array("FontName"=>$font_folder.DS.'pf_arma_five.ttf',"FontSize"=>8));

        // Defino el area del grafico
        $myPicture->setGraphArea(50,30,980,250);

        // Dibuja escala del grafico
        /* Draw the scale */
        $scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>FALSE,"CycleBackground"=>TRUE, 'LabelRotation'=>45,
                                "Mode" => SCALE_MODE_START0, "XMargin" => 40, "MinDivHeight" => 50);
        $myPicture->drawScale($scaleSettings);
      
        // Turn on AntiAliasing 
        $myPicture->Antialias = TRUE;
        
        // Dibujo el grafico
        $settings = array("BarWidth" => 30, "Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_TOP,"DisplayValues"=>TRUE,
                            "DisplayR"=>0,"DisplayG"=>0,"DisplayB"=>0,"DisplayShadow"=>TRUE,
                            "Surrounding"=>10);        
        $myPicture->drawBarChart($settings);        
        // genero la imagen que muestra la pagina
        $myPicture->autoOutput(APP.'Vendor'.DS.'pchart'.DS.'pictures'.DS.'facturacion.jpeg');        
        exit;         
    }
    
     
 }
?>
